package bb.lanxing.util.ui;

import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.core.graphics.drawable.DrawableCompat;

public class DrawableUtils {
    public static void setBackgroundTint(View view, int i) {
        if (Build.VERSION.SDK_INT >= 21) {
            view.setBackgroundTintList(ColorStateList.valueOf(i));
            return;
        }
        Drawable tint = tint(view.getBackground(), i);
        if (Build.VERSION.SDK_INT >= 16) {
            view.setBackground(tint);
        } else {
            view.setBackgroundDrawable(tint);
        }
    }

    public static void setBackgroundTint(View view, ColorStateList colorStateList) {
        if (Build.VERSION.SDK_INT >= 21) {
            view.setBackgroundTintList(colorStateList);
            return;
        }
        Drawable tint = tint(view.getBackground(), colorStateList);
        if (Build.VERSION.SDK_INT >= 16) {
            view.setBackground(tint);
        } else {
            view.setBackgroundDrawable(tint);
        }
    }

    public static void clearBackgroundTint(View view, Drawable drawable) {
        if (Build.VERSION.SDK_INT >= 21) {
            view.setBackgroundTintList(null);
        }
        if (Build.VERSION.SDK_INT < 16) {
            view.setBackgroundDrawable(drawable);
        } else {
            view.setBackground(drawable);
        }
    }

    public static void setTint(ImageView imageView, int i) {
        if (Build.VERSION.SDK_INT >= 21) {
            imageView.getDrawable().setTint(i);
        } else {
            imageView.setImageDrawable(tint(imageView.getDrawable(), i));
        }
    }

    public static void setTint(ImageView imageView, ColorStateList colorStateList) {
        if (Build.VERSION.SDK_INT >= 21) {
            imageView.getDrawable().setTintList(colorStateList);
        } else {
            imageView.setImageDrawable(tint(imageView.getDrawable(), colorStateList));
        }
    }

    public static Drawable tint(Drawable drawable, int i) {
        if (drawable == null) {
            return null;
        }
        if (i == 0) {
            return drawable;
        }
        Rect bounds = drawable.getBounds();
        Drawable wrap = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(wrap.mutate(), i);
        wrap.setBounds(bounds);
        return wrap;
    }

    public static Drawable tint(Drawable drawable, ColorStateList colorStateList) {
        if (drawable == null) {
            return null;
        }
        Rect bounds = drawable.getBounds();
        Drawable wrap = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(wrap.mutate(), colorStateList);
        wrap.setBounds(bounds);
        return wrap;
    }

    /*
    *  View.MeasureSpec.AT_MOST
    * View.MeasureSpec.UNSPECIFIED
    * View.MeasureSpec.EXACTLY
     */
    public static Drawable convertDrawable(View view) {
        int makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int makeMeasureSpec2 = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams != null) {
            if (layoutParams.width > 0) {
                makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(layoutParams.width, View.MeasureSpec.EXACTLY);
            } else if (layoutParams.width == -1) {
                makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.AT_MOST);
            } else if (layoutParams.width == -2) {
                makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            }
            if (layoutParams.height > 0) {
                makeMeasureSpec2 = View.MeasureSpec.makeMeasureSpec(layoutParams.height, View.MeasureSpec.EXACTLY);
            } else if (layoutParams.height == -1) {
                makeMeasureSpec2 = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.AT_MOST);
            } else if (layoutParams.height == -2) {
                makeMeasureSpec2 = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            }
        }
        view.measure(makeMeasureSpec, makeMeasureSpec2);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        BitmapDrawable bitmapDrawable = new BitmapDrawable(view.getResources(), Bitmap.createBitmap(view.getDrawingCache()));
        view.destroyDrawingCache();
        return bitmapDrawable;
    }
}
